package com.compty.paint.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.framework.util.DateUtil;
import com.compty.paint.domain.Bid;
import com.compty.paint.domain.BidRecord;
import com.compty.paint.domain.CityInfo;
import com.compty.paint.domain.CustomerInfo;
import com.compty.paint.domain.Goods;
import com.compty.paint.domain.Order;
import com.compty.paint.domain.OrderItem;
import com.compty.paint.domain.Painting;
import com.compty.paint.domain.ProvinceInfo;
import com.compty.paint.manager.BidManager;
import com.compty.paint.manager.CityInfoManager;
import com.compty.paint.manager.CustomerInfoManager;
import com.compty.paint.manager.GoodsManager;
import com.compty.paint.manager.OrderManager;
import com.compty.paint.manager.PaintingManager;
import com.compty.paint.manager.ProvinceInfoManager;

/**
 *竟拍结束(英式拍卖)处理service.生成订单
 */
public class BidDealManager {
	
	private BidManager bidManager;
	private OrderManager orderManager;
	private GoodsManager goodsManager;
	private PaintingManager paintingManager;
	
	/**
	 * 拍卖结束后,为竞拍成功的用户生成订单
	 */
	public void buildOrder(){
		List<Bid>  bidList = bidManager.queryBid_end();//查询结束竟拍未生成订单的拍品列表
		for (int i = 0; i < bidList.size(); i++) {
			Bid bid = bidList.get(i);
			Date endTime = bid.getEndTime();//竟拍结束时间
			if(DateUtil.getNow().compareTo(endTime) >= 0){
				
				updateBidStatus(bid);//更新拍品信息
				
				Integer bid_status = bid.getBid_status();
				if( 2 == bid_status){//如果状态为已拍出
					
					Order order = generateOrder(bid);
					bid.setOrder(order);
				}
				
				bidManager.save(bid);
			}
		}
	}
	
	/**
	 * 创建订单(为结束竟拍的拍品生成订单)
	 * @param bid 拍品
	 */
	private Order generateOrder(Bid bid) {
		Order order = new Order();
		
		BidRecord bidRecord = bid.getCurrentBidRecord();//加价记录
		
		/******************** 订单客户基本信息  begin  ************************/
		CustomerInfo customerInfo = bidRecord.getCustomerInfo();
		CustomerInfoManager customerInfoManager = SpringContextHolder.getBean("customerInfoManager");
		customerInfo = customerInfoManager.get(customerInfo.getId());
		
		Long pId = customerInfo.getProvinceInfo().getId();
		ProvinceInfoManager provinceInfoManager = SpringContextHolder.getBean("provinceInfoManager");
		ProvinceInfo provinceInfo = provinceInfoManager.get(pId);
		
		Long cId = customerInfo.getCityInfo().getId();
		CityInfoManager cityInfoManager = SpringContextHolder.getBean("cityInfoManager");
		CityInfo cityInfo = cityInfoManager.get(cId);
		/******************** 订单客户基本信息  end  ************************/
		
		Integer goods_id = bid.getGoods_id();
		Goods goods = goodsManager.get(goods_id);
		Long  painting_id = goods.getPainting_id();
		Painting painting = paintingManager.get(painting_id);
		
		/***************** 订单明细 begin  **************************/
		List<OrderItem> orderItems = new ArrayList<OrderItem>(0);
		OrderItem orderItem = new OrderItem();
		orderItem.setNum(1);
		orderItem.setShip_num(1);
		orderItem.setName(goods.getName());
		orderItem.setPrice(bid.getKnockDownPrice());
		orderItem.setSn(goods.getSn());
		orderItem.setBelongTo(painting.getEntryPerson().getId());
		orderItem.setGoods(goods);
		orderItem.setOrder(order);
		orderItems.add(orderItem);
		/****************** 订单明细   end *************************/
		
		/******************* 订单属性设置  begin ************************/
		order.setStatus(0);//未付款
		order.setPay_status(0);//未支付
		order.setShip_status(0);//未发货
		order.setDisabled(1);//可用，有效
		
		order.setOrder_amount(orderItem.getPrice());
		order.setGoods_amount(orderItem.getPrice());
		
		/******************** 设置订单物流信息 begin ***************************/
		order.setShip_addr(provinceInfo.getName()+cityInfo.getName()+customerInfo.getAddress());
		order.setShip_email(customerInfo.getEmail());
		order.setShip_mobile(customerInfo.getMobile());
		order.setShip_name(customerInfo.getCustomerName());
		order.setShip_tel(customerInfo.getTelphone());
		order.setShip_zip(customerInfo.getZipCode());
		order.setShipping_amount(0.0);
		order.setGoods_num(1);
		order.setGoods(painting.getTitle());
		/******************** 设置订单物流信息 end ***************************/
			
		order.setCustomerInfo(customerInfo);
		order.setModifyTime(Calendar.getInstance().getTime());
		order.setOrderTime(Calendar.getInstance().getTime());
		order.setOrderItems(orderItems);
		
		String sequenceNo = OrderManager.generateSequenceNo(customerInfo);//订单号
		order.setSequenceNo(sequenceNo);
		
		/******************* 订单属性设置  end ************************/
		
		orderManager.save(order);//保存订单
		
		return order;
	}

	/**
	 * 更新拍品信息(拍卖状态及落槌价)
	 * @param bid 拍品
	 */
	private Bid updateBidStatus(Bid bid){
		
		BidRecord bidRecord  = bid.getCurrentBidRecord();
		if(bidRecord != null){
			double bidPrice = bidRecord.getBidPrice();
			bid.setKnockDownPrice(bidPrice);
			bid.setBid_status(2);//已拍出
		}else{
			bid.setBid_status(3);//流拍
		}
		
		return bid;
	}

	public void setBidManager(BidManager bidManager) {
		this.bidManager = bidManager;
	}

	public void setOrderManager(OrderManager orderManager) {
		this.orderManager = orderManager;
	}

	public void setGoodsManager(GoodsManager goodsManager) {
		this.goodsManager = goodsManager;
	}

	public void setPaintingManager(PaintingManager paintingManager) {
		this.paintingManager = paintingManager;
	}

}
